//
//  SLMenuLabel.m
//  MenuViewController
//
//  Created by superlian on 16/8/11.
//  Copyright © 2016年 superlian. All rights reserved.
//

#import "SLMenuLabel.h"

@implementation SLMenuLabel

//纯代码 执行
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.menuItemType = kSLMenuItemTypeDefault;
        self.gestureType = kSLGestureTypeLongGesture;
        self.userInteractionEnabled = YES;
        [self setUpMenuItem];
    }
    return self;
}
//xib 执行
-(void)awakeFromNib {
    
    self.menuItemType = kSLMenuItemTypeDefault;
    self.gestureType = kSLGestureTypeLongGesture;
    self.userInteractionEnabled = YES;
    [self setUpMenuItem];
}


-(void)setUpMenuItem {
    if (self.gestureType == kSLGestureTypeLongGesture) {
        UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGestAction:)];
        [self addGestureRecognizer:longTap];
    }else if (self.gestureType == kSLGestureTypeTapGestture) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [self addGestureRecognizer:tap];
    }
}

#pragma mark - gestureAction
-(void)longGestAction:(UILongPressGestureRecognizer *)longGest {
        if (longGest.state == UIGestureRecognizerStateBegan) {
            switch (self.menuItemType) {
                case kSLMenuItemTypeDefault: {
                    [self configDefaultMenuItem];
                    break;
                }
                case kSLMenuItemTypeCustom: {
                    [self configCustomMenuItem];
                    break;
                }
            }
        }
}
-(void)tapAction:(UITapGestureRecognizer *)tap {
    
    switch (self.menuItemType) {
        case kSLMenuItemTypeDefault: {
            [self configDefaultMenuItem];
            break;
        }
        case kSLMenuItemTypeCustom: {
            [self configCustomMenuItem];
            break;
        }
    }
}

#pragma mark - 配置menuItem
//使用系统的
-(void)configDefaultMenuItem {
    //label成为第一响应者
    [self becomeFirstResponder];
    UIMenuController *menu = [UIMenuController sharedMenuController];
    //设置显示位置
    [menu setTargetRect:self.bounds inView:self];
    //显示
    [menu setMenuVisible:YES animated:YES];
}

-(void)configCustomMenuItem {
    [self becomeFirstResponder];
    UIMenuController *menu = [UIMenuController sharedMenuController];
    NSAssert(self.menuArray == nil, @"menuArray can not be nil!");
    NSMutableArray *menuItemAry = [NSMutableArray arrayWithCapacity:self.menuArray.count];
    for (int i = 0; i < self.menuArray.count; i++) {
        UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:self.menuArray[i] action:@selector(menuItemAction:)];
        [menuItemAry addObject:menuItem];
    }
    menu.menuItems = menuItemAry;
    //设置显示位置
    [menu setTargetRect:self.bounds inView:self];
    //显示
    [menu setMenuVisible:YES animated:YES];
}

#pragma mark - UIMenuController相关
-(BOOL)canBecomeFirstResponder {
    return YES;
}
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    //需要 copy，paste，delete的 这些 items
    if (
        (action == @selector(copy:) && self.text)
        || (action == @selector(cut:) && self.text)
        || action == @selector(paste:)
        || action == @selector(delete:)
        || action == @selector(menuItemAction:)
        )
    {
        return YES;
    }else{
        [super canPerformAction:action withSender:sender];
    }
    return NO;
}

#pragma mark - 点击事件监听
-(void)cut:(UIMenuController *)menu {
    [UIPasteboard generalPasteboard].string = self.text;
    self.text = nil;
}
-(void)copy:(UIMenuController *)menu {
    [UIPasteboard generalPasteboard].string = self.text;
}
-(void)paste:(UIMenuController *)menu {
    self.text = [UIPasteboard generalPasteboard].string;
}
-(void)delete:(UIMenuController *)menu {
    self.text = nil;
}

-(void)menuItemAction:(UIMenuItem *)item {
    if (self.custemBlock) {
        self.custemBlock(item.title);
    }
}

#pragma mark - block
-(void)getMenuItemWithBlock:(CustomItemBlock)customBlock {
    if (customBlock) {
        self.custemBlock = customBlock;
    }
}

#pragma mark - 设置

-(void)setMenuItemType:(SLMenuItemType)menuItemType {
    
    //    if (menuItemType == nil) {
    //
    //    }
    _menuItemType = menuItemType;
}
-(void)setGestureType:(SLGestureType)gestureType {
    _gestureType = gestureType;
}

@end
